package models

import (
	"gitchina/kly_service/data"
	"gitchina/kly_service/errors"
	"gitchina/kly_service/util"
	"strconv"
	"strings"
)

type AddOrUpdateLogisticsCorpRQ struct {
	CorpName  string `json:"corp_name"`
	CorpAddr  string `json:"corp_addr"`
	Telephone string `json:"telephone"`
}

type LogisticsCorpRS struct {
	Name       string `json:"name"`
	CorpName   string `json:"corp_name"`
	CorpAddr   string `json:"corp_addr"`
	Telephone  string `json:"telephone"`
	Remark     string `json:"remark"`
	Area       int    `json:"area"`
	BizLicense string `json:"biz_license"`
	BizPermit  string `json:"biz_permit"`
}

type LogisticsBiz struct {
	Intro       string `json:"intro"`
	BizAddr     string `json:"biz_addr"`
	ServiceType int    `json:"service_type"`
}

type InvoiceCorp struct {
	Id     int    `json:"corp_id"`
	Name   string `json:"corp_name"`
	AreaId int    `json:"area_id"`
	Addr   string `json:"corp_addr"`
}

type GetLogisticsCorpListRQ struct {
	AreaId   int `json:"area_id"`
	PageNo   int `json:"page_no"`
	PageSize int `json:"page_size"`
}

type LogisticsCorpListRS struct {
	TotalCount int                     `json:"total_count"`
	List       []LogisticsCorpDetailRS `json:"corp_list"`
}

type GetLogisticsCorpDetailRQ struct {
	Id ID `json:"emp_id"`
}

type LogisticsCorpDetailRS struct {
	OwnerName  string `json:"owner_name"`
	OwnerPhone string `json:"owner_phone"`
	OwnerPhoto string `json:"owner_photo"`
	EmpId      ID     `json:"emp_id"`
	CorpName   string `json:"corp_name"`
	Intro      string `json:"intro"`
}

func (l Logistics) AddOrUpdateLogisticsCorp(mbrId ID, rq AddOrUpdateLogisticsCorpRQ) (id int64, err error) {
	var repo data.LogisticsRepo
	_, _, _, id, err = mbrId.Decode()
	if err != nil {
		util.GetLogger().Error("[model-AddOrUpdateLogisticsCorp] - error. unexpected mbrId:%s", string(mbrId))
		err = errors.New(errors.MSG_Mbr_Invalid)
	} else {
		id, err = repo.AddOrUpdateLogisticsCorp(&data.LogisticsEmpDto{
			Id: int(id),
		}, &data.LogisticsCorpDto{
			Name:      rq.CorpName,
			Addr:      rq.CorpAddr,
			Telephone: rq.Telephone,
		})
		if err != nil {
			util.GetLogger().Error("[model-AddOrUpdateLogisticsCorp] - error: %s", err.Error())
		}
	}
	return
}

func (l Logistics) GetLogisticsCorp(mbrId ID) (rs LogisticsCorpRS) {
	var repo data.LogisticsRepo
	_, _, _, id, err := mbrId.Decode()
	if err != nil {
		util.GetLogger().Error("[model-GetLogisticsCorp] - error. unexpected mbrId:%s", string(mbrId))
		err = errors.New(errors.MSG_Mbr_Invalid)
	} else {
		if dto, err := repo.GetLogisticsCorp(int(id)); err == nil {
			rs = LogisticsCorpRS{
				Name:       dto.Name,
				CorpName:   dto.CorpName,
				CorpAddr:   dto.CorpAddr,
				Telephone:  dto.Telephone,
				Remark:     dto.Remark,
				Area:       dto.AreaId,
				BizLicense: dto.BizLicense,
				BizPermit:  dto.BizPermit,
			}
		} else {
			util.GetLogger().Error("[model-GetLogisticsCorp] - error: %s", err.Error())
			err = nil
		}
	}
	return
}

func (l Logistics) GetLogisticsCorpList(rq *GetLogisticsCorpListRQ) (rs LogisticsCorpListRS) {
	var rowCount int
	var err error
	var list []data.LogisticsCorpDetailDto
	var repo data.LogisticsRepo
	if rq.PageNo <= 0 {
		rq.PageNo = 1
	}

	if rq.PageSize <= 0 || rq.PageSize > 20 {
		rq.PageSize = 10
	}

	var corpList []LogisticsCorpDetailRS
	if rq.AreaId%100 == 0 {
		areaId := strings.TrimRight(strconv.Itoa(rq.AreaId), "0")
		if list, rowCount, err = repo.QueryLogisticsCorpList(areaId, rq.PageNo, rq.PageSize); err == nil {
			corpList = make([]LogisticsCorpDetailRS, len(list))
			for idx, dto := range list {
				var mbrId ID
				mbrId.Encode(ID_Shard, ID_Ver, ID_Member_Category, int64(dto.OwnerId))
				corpList[idx] = LogisticsCorpDetailRS{
					OwnerName:  dto.OwnerName,
					OwnerPhone: dto.OwnerPhone,
					OwnerPhoto: dto.OwnerPhoto,
					EmpId:      mbrId,
					CorpName:   dto.CorpName,
					Intro:      dto.Intro,
				}
			}
		} else {
			util.GetLogger().Error("[model-GetLogisticsCorpList] - error:%s", err.Error())
		}
	} else {
		if list, rowCount, err = repo.GetLogisticsCorpList(rq.AreaId, rq.PageNo, rq.PageSize); err == nil {
			corpList = make([]LogisticsCorpDetailRS, len(list))
			for idx, dto := range list {
				var mbrId ID
				mbrId.Encode(ID_Shard, ID_Ver, ID_Member_Category, int64(dto.OwnerId))
				corpList[idx] = LogisticsCorpDetailRS{
					OwnerName:  dto.OwnerName,
					OwnerPhone: dto.OwnerPhone,
					OwnerPhoto: dto.OwnerPhoto,
					EmpId:      mbrId,
					CorpName:   dto.CorpName,
					Intro:      dto.Intro,
				}
			}
		} else {
			util.GetLogger().Error("[model-GetLogisticsCorpList] - error:%s", err.Error())
		}
	}
	rs = LogisticsCorpListRS{TotalCount: rowCount, List: corpList}
	return
}

func (l Logistics) GetLogisticsCorpDetail(mbrId ID) (rs LogisticsCorpDetailRS) {
	var id int64
	var repo data.LogisticsRepo
	_, _, _, id, err := mbrId.Decode()
	if err != nil {
		util.GetLogger().Error("[model-GetLogisticsCorpDetail] - error. unexpected mbrId:%s", string(mbrId))
		err = errors.New(errors.MSG_Mbr_Invalid)
	} else {
		if dto, err := repo.GetLogisticsCorpDetail(int(id)); err == nil {
			rs = LogisticsCorpDetailRS{
				OwnerName:  dto.OwnerName,
				OwnerPhone: dto.OwnerPhone,
				OwnerPhoto: dto.OwnerPhoto,
				EmpId:      mbrId,
				CorpName:   dto.CorpName,
				Intro:      dto.Intro,
			}
		} else {
			util.GetLogger().Error("[model-GetLogisticsCorpDetail] - error: %s", err.Error())
			err = nil
		}
	}
	return
}

func (l Logistics) UpdateLogisticsBiz(mbrId ID, rq *LogisticsBiz) {
	var err error
	var id int64
	var repo data.LogisticsRepo
	if _, _, _, id, err = mbrId.Decode(); err != nil {
		util.GetLogger().Error("[model-UpdateLogisticsBiz] - error. unexpected mbrId:%s", string(mbrId))
		err = errors.New(errors.MSG_Mbr_Invalid)
	} else {
		if err := repo.UpdateLogisticsBiz(&data.LogisticsBizDto{
			Id:          int(id),
			Intro:       rq.Intro,
			BizAddr:     rq.BizAddr,
			ServiceType: rq.ServiceType,
		}); err != nil {
			util.GetLogger().Error("[model-UpdateLogisticsBiz] - error:%s", err.Error())
		}
	}
}

func (l Logistics) GetLogisticsBiz(mbrId ID) (rs LogisticsBiz) {
	if _, _, _, id, err := mbrId.Decode(); err != nil {
		util.GetLogger().Error("[model-GetLogisticsBiz] - error. unexpected mbrId:%s", string(mbrId))
		err = errors.New(errors.MSG_Mbr_Invalid)
	} else {
		var repo data.LogisticsRepo
		if bizDto, err := repo.GetLogisticsBiz(int(id)); err != nil {
			util.GetLogger().Error("[model-GetLogisticsBiz] - error:%s", err.Error())
			err = nil
		} else {
			rs = LogisticsBiz{
				Intro:       bizDto.Intro,
				ServiceType: bizDto.ServiceType,
				BizAddr:     bizDto.BizAddr,
			}
		}
	}
	return
}

func (l Logistics) GetInvoiceCorps() []InvoiceCorp {
	list := make([]InvoiceCorp, len(invoiceCorp_map))
	idx := 0
	for _, v := range invoiceCorp_map {
		list[idx] = v
		idx++
	}
	return list
}

func (l Logistics) GetInvoiceCorp(id string) (InvoiceCorp, bool) {
	c, ok := invoiceCorp_map[id]
	return c, ok
}
